package com.jd.jobkeeper.client.http;

import java.util.HashMap;
import java.util.Map;

/**
 * HTTP请求
 */
public class HttpRequest {

    public static final String LAST_MODIFIED = "Last-Modified";
    public static final String IF_MODIFIED_SINCE = "If-Modified-Since";
    public static final String CONTENT_TYPE = "Content-Type";
    public static final String USER_AGENT = "User-Agent";
    public static final String CLIENT_IP = "Client-Ip";
    public static final String GZIP = "gzip";
    public static final String DEFLATE = "deflate";
    public static final String ACCEPT_ENCODING = "Accept-Encoding";
    public static final String ACCEPT = "Accept";
    public static final String GET = "GET";
    public static final String CHARSET_NAME = "UTF-8"; //服务端交互的编码

    public static final String APPLICATION_JSON = "application/json;charset=" + CHARSET_NAME;
    public static final String APPLICATION_PROPERTIES = "application/properties;charset=" + CHARSET_NAME;
    public static final String TOKEN = "token";
    public static final String APPLICATION = "application";
    //请求地址
    protected String uri;
    protected String httpMethod = GET;
    //连接超时
    protected int connectTimeout = 5000;
    //数据包超时
    protected int socketTimeout = 5000;

    protected String charsetName = CHARSET_NAME;

    protected Map<String, String> headers = new HashMap<>();

    public HttpRequest() {
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getCharsetName() {
        return charsetName;
    }

    public void setCharsetName(String charsetName) {
        this.charsetName = charsetName;
    }

    public String getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(String httpMethod) {
        this.httpMethod = httpMethod;
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public void setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
    }

    public int getSocketTimeout() {
        return socketTimeout;
    }

    public void setSocketTimeout(int socketTimeout) {
        this.socketTimeout = socketTimeout;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void addHeader(String key, String value) {
        if (key != null && !key.isEmpty() && value != null && !value.isEmpty()) {
            headers.put(key, value);
        }
    }

    public void addHeader(String key, long value) {
        addHeader(key, String.valueOf(value));
    }

    public void addHeader(String key, int value) {
        addHeader(key, String.valueOf(value));
    }

    public void addHeader(String key, boolean value) {
        addHeader(key, String.valueOf(value));
    }
}
